package com.uyaer.wheel.menu
{
	import com.uyaer.wheel.common.Common;
	import com.uyaer.wheel.common.UserData;
	import com.uyaer.wheel.events.EventType;
	import com.uyaer.wheel.res.ResURI;
	import com.uyaer.wheel.story.Story;
	
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.text.BitmapFont;
	import starling.text.TextField;
	import starling.textures.Texture;

	public class Menu extends Sprite
	{
		[Embed(source="/../assets/font.png")]
		private var Font:Class;
		[Embed(source="/../assets/font.fnt", mimeType="application/octet-stream")]
		private var FontXML:Class;
		[Embed(source="/../assets/lvFont.png")]
		private var LvFont:Class;
		[Embed(source="/../assets/lvfont.fnt", mimeType="application/octet-stream")]
		private var LvFontXML:Class;
		[Embed(source="/../assets/supreme.ttf",embedAsCFF="false",fontFamily="Supreme")]
		private var HitFont:Class;
		
		private var _type:String;
		public function Menu()
		{
			if(stage){
				onAdded();
			}else{				this.addEventListener(Event.ADDED_TO_STAGE,onAdded);
			}
		}
		
		private function onAdded():void{
			this.removeEventListener(Event.ADDED_TO_STAGE,onAdded);
			Story.instance.regiest(this);
			init();
			
			stage.addEventListener(EventType.MENU_SELECT,onTypeChange);
			stage.addEventListener(EventType.MENU_SELECT,onTypeChange);
		}
		
		private function onTypeChange(e:Event):void{
			var t:String = e.data as String;
			if(t == MenuType.SHOW_HOME){
				stage.dispatchEventWith(EventType.DESTORY_GAME,false,true);
			}else if(t == MenuType.REPLAY_GAME){
				stage.dispatchEventWith(EventType.DESTORY_GAME,false,false);
			}
			type = t;
		}
		
		public function get type():String{
			return _type;
		}

		public function set type(value:String):void{
			_type = value;
			switch(type)
			{
				case MenuType.SHOW_HOME:
					overHomeCallback();
					break;	
				case MenuType.START_GAME:
					this.startGame();
					break;
				case MenuType.GAME_INFO:
					Story.instance.showGameInfo();
					break;
				case MenuType.PAUSE_GAME:
					Story.instance.showPause();
					break;
				case MenuType.RESUME_GAME:
					Story.instance.hidePause();
					stage.dispatchEventWith(EventType.GAME_RESUME);
					break;
				case MenuType.REPLAY_GAME:
					replayGame();
					break;
				case MenuType.DIE_GAME:
					Story.instance.showGameOver();
					break;
			}
		}

		private function init():void{
			Story.instance.showLoading(function():void{
				//初始化文字
				var fontTexture:Texture = Texture.fromBitmap(new Font());
				var xml:XML = XML(new FontXML());
				TextField.registerBitmapFont(new BitmapFont(fontTexture,xml),Common.FONT);
				
				fontTexture = Texture.fromBitmap(new LvFont());
				xml = XML(new LvFontXML());
				TextField.registerBitmapFont(new BitmapFont(fontTexture,xml),Common.LVFONT);
				
				//游戏素材
				new ResURI().init();
				
				type = MenuType.SHOW_HOME;
			});
		}
		
		private function replayGame():void{
			this.reset();
			UserData.me.init();
			stage.dispatchEventWith(EventType.START_GAME);
			type = MenuType.GAME_INFO;
			Story.instance.hideLoading(function():void{
				stage.dispatchEventWith(EventType.RUN_GAME);
			}); 
		}
		
		private function startGame():void{
			Story.instance.showLoading(function():void{
				UserData.me.init();
				overGameCallback();
			});
		}
		
		/**
		 *过程回调 
		 */		
		private function progressCallback(d:Number):void{
			Story.instance.updateLoading(d);
		}
		
		/**
		 *资源加载完成回调 
		 */		
		private function overHomeCallback():void{
			this.reset();
			Story.instance.hideLoading(null);
			Story.instance.showHome();
		}		
		
		private function overGameCallback():void{
			Story.instance.hideHome();
			stage.dispatchEventWith(EventType.START_GAME);
			type = MenuType.GAME_INFO;
			Story.instance.hideLoading(function():void{
				stage.dispatchEventWith(EventType.RUN_GAME);
			}); 
		}
		
		private function reset():void{
			Story.instance.hideGameInfo();
			Story.instance.hidePause();
			Story.instance.hideGameOver();
		}
		
		public function showAlert(txt:String,callback:Function):void{
			Story.instance.showAlert(txt,callback);
		}
	}
}